# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = "restructuredtext en"
__all__ = []

from ... import pilelib as stack
from ...forms import freecell as flayout
from ...gamelib import info as gi, game
from ...packlib import info as cs
from ...piles import tarock as tstack
from ...utillib.misc import kwdefault
from .. import katzenschwanz

Wrap = stack.StackWrapper
GT, SL = gi.GT, gi.SL


class Skiz(cs.Tarock_Deck, game.Game):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.freecell.GloriaLayout

        - row [12 horz S foundation, vert offset]
        - talon [1 SW, no offset]
        - reserve [3 NE 3 NW, no offset]
        - foundation [5 horz N, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.HighRK_SS_RowStack

        Init 6 open card outer 6, 7 open card inner 6. Build down in rank by
        same suit. Play rank alone sequence. Base king or high trump.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=160, name="Skiz", category=cs.TYPE_TAROCK,
                    game_type=(GT.FREECELL | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='4.00')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _layout_ = flayout.GloriaLayout
    _talon_ = stack.InitialDealTalon
    _foundation_ = stack.SS_Foundation
    _row_ = tstack.HighRK_SS_RowsStack
    _reserve_ = stack.ReserveStack

    def _create_game(self, **kw):
        l = _layout_()
        suits = l.SUIT
        bases = suits[:2] + [4] + suits[1:-2]
        kwdefault(kw, rows=12, reserves=6, bases=bases)
        return super(Skiz, self)._create_game(**kw)

    def _start(self, rows=7, **kw):
        """
        Deal initial cards for new game.
          - s[rows] 6 ea up.
          - s[rows][3:9] 1 ea up.

        .. \_start([rows:int=7]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(frames=0)
        self.s['talon'].dealRow(rows=self.s['rows'][3:9])


class Pagat(Skiz):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.freecell.GloriaLayout

        - row [12 horz S foundation, vert offset]
        - talon [1 SW, no offset]
        - reserve [3 NE 3 NW, no offset]
        - foundation [5 horz N, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.RK_SS_RowStack

        Init 6 open card outer 6, 7 open card inner 6. Build down in rank by
        same suit. Play rank alone sequence.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=159, name="Pagat", category=cs.TYPE_TAROCK,
                    game_type=(GT.FREECELL | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='4.00')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _row_ = tstack.RK_SS_RowStack


class Corkscrew(cs.Tarock_Deck, katzenschwanz.DerKatzenschwanz):
    """
    Finish 2 deck on foundation.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.CatsTailLayout

        - row [11 horz S reserve, vert offset]
        - talon [1 SW, no offset]
        - reserve [10 N, no offset]
        - foundation [10 over 2 col E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.RK_RowStack (base_rank=GAP)

        Init all cards with king or high trump start pile. Build down in
        rank alone. Play sequence. Base none.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.games.katzenshwanz.DerKatzenschwanzHint
    """
    __info__ = dict(id=13165, name='Corkscrew', category=cs.TYPE_TAROCK,
                    game_type=GT.FREECELL, decks=2, version='0.50fc',
                    skill_level=SL.MOSTLY_SKILL)
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _row_ = Wrap(stack.RK_RowStack, base_rank=cs.GAP)

    def _create_game(self, **kw):
        kwdefault(kw, rows=11, reserves=10)
        return super(Corkscrew, self)._create_game(**kw)

    def _start(self, **kw):
        """
        Deal initial cards for new game.
          - s[rows] all up. next KING/ 21 TRUMP.

        .. \_start([]) -> None
        """
        isKing = lambda c: c['rank'] == 13 + 8 * (c['suit'] == 4)
        i = 0
        while s['talon'].cards:
            if isKing(self.s['talon'].TOPCARD) and self.s['rows'][i].cards:
                    i += 1
            self.s['talon'].dealRow(rows=[self.s['rows'][i]], frames=4)


class Serpent(Corkscrew):
    """
    Finish 2 deck on foundation.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.CatsTailLayout

        - row [11 horz S reserve, vert offset]
        - talon [1 SW, no offset]
        - reserve [10 N, no offset]
        - foundation [10 over 2 col E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpAC_RowStack  (base_rank=GAP)

        Init all cards with king or high trump start pile. Build down in
        rank by alt color and trump by rank alone. Play sequence. Base none.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.games.katzenshwanz.DerKatzenschwanzHint
    """
    __info__ = dict(id=13166, name='Serpent', category=cs.TYPE_TAROCK,
                    game_type=GT.FREECELL, decks=2, version='0.50fc',
                    skill_level=SL.MOSTLY_SKILL)
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _row_ = Wrap(tstack.TrumpAC_RowStack, base_rank=cs.GAP)


class Rambling(Corkscrew):
    """
    Finish 2 deck on foundation.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.CatsTailLayout

        - row [11 horz S reserve, vert offset]
        - talon [1 SW, no offset]
        - reserve [10 N, no offset]
        - foundation [10 over 2 col E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stack.SS_RowStack (base_rank=GAP)

        Init all cards with king or high trump start pile. Build down in
        rank by same suit. Play sequence. Base none.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.games.katzenshwanz.DerKatzenschwanzHint
    """
    __info__ = dict(id=13167, name='Rambling', category=cs.TYPE_TAROCK,
                    game_type=GT.FREECELL, decks=2, version='0.50fc',
                    skill_level=SL.MOSTLY_SKILL)
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _row_ = Wrap(stack.SS_RowStack, base_rank=cs.GAP)


PROVIDES = [Pagat, Skiz]